package com.we823.cms.controller.manage;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.utils.StringUtil;
import com.we823.cms.model.sys.Config;
import com.we823.cms.model.sys.Resource;
import com.we823.cms.model.sys.ResourceType;
import com.we823.cms.model.sys.RoleResourcePermission;
import com.we823.cms.model.sys.User;
import com.we823.cms.repository.service.sys.ConfigService;
import com.we823.cms.repository.service.sys.ResourceService;
import com.we823.cms.repository.service.sys.RoleResourcePermissionService;

@Controller
@RequestMapping(WebConstants.WEB_PREFIX+WebConstants.WEB_MANAGE_URL)
public class IndexManageController {

	private String baseUrl = WebConstants.WEB_MANAGE_TEMPLATE_BaseUrl;

	private @javax.annotation.Resource RoleResourcePermissionService roleResourcePermissionService;
	private @javax.annotation.Resource ResourceService resourceService;
	private @javax.annotation.Resource ConfigService configService;
	
	@RequestMapping("index")
	public String index(HttpServletRequest request,Model model){
		HttpSession session = request.getSession();
		User userModel = (User) session.getAttribute(WebConstants.CURRENT_USER);
		if (userModel!=null) {
			model.addAttribute("username",userModel.getUsername());
			model.addAttribute("user",userModel);
			createMenu(model);
			
			List<Config> configModels = configService.findByIsCurrent(1);
			if (configModels!=null && configModels.size()>0) {
				model.addAttribute("config",configModels.get(0));
			}
			
			
		}else {
			Subject subject = SecurityUtils.getSubject();
			subject.logout();
		}
		
		return baseUrl + "index";
	}
	
	private void createMenu(Model model) {
		//1.get user and roleIds
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User userModel = (User) session.getAttribute(WebConstants.CURRENT_USER);
		String roleIds = null;
		if (userModel!=null) {
			roleIds = userModel.getRoleIds();
		}
		
		//2.获取roleIds
		if (roleIds!=null) {
			List<Integer> roleIdList = StringUtil.stringToIntList(roleIds);
			
			List<RoleResourcePermission> roleResourcePermissionModels = roleResourcePermissionService.findByRoleIdIn(roleIdList);
			if (roleResourcePermissionModels!=null) {
				//3.获取全部resource，menu
				List<Resource> mainResources = resourceService.findByParentidAndType(0, ResourceType.MENU, true);
				Set<Integer> resourceSet = null;
				if (mainResources!=null && roleResourcePermissionModels!=null) {
					resourceSet = Sets.newHashSet();
					for (RoleResourcePermission roleResourcePermissionModel : roleResourcePermissionModels) {
						resourceSet.add(roleResourcePermissionModel.getResourceId());
					}
				}
				List<Resource> finalMenu = buildFinalMenu(mainResources,resourceSet);
				model.addAttribute("menu",finalMenu);
			}
		}
		
	}

	private List<Resource> buildFinalMenu(
			List<Resource> mainResources,
			Set<Integer> resourceSet) {
		List<Resource> finalMenu = Lists.newArrayList();
		if (mainResources!=null && mainResources.size()>0) {
			for (Resource resourceModel : mainResources) {
				if (resourceSet.contains(resourceModel.getId())) {
					Resource newResourceModel = Resource.deepCopy(resourceModel);
					List<Resource> children = resourceModel.getSubresources();
					if (children!=null && children.size()>0) {
						List<Resource> newChildren = buildFinalMenu(children, resourceSet);
						newResourceModel.setSubresources(newChildren);
					}
					finalMenu.add(newResourceModel);
				}
			}
		}
		
		return finalMenu;
	}

	@RequestMapping("logout")
	public String logout(HttpServletRequest request,Model model) {
		String resutlUrl = RequestUtil.getValueByName(request, "returnUrl",request.getContextPath()+"/index.jspx");
		return "redirect:"+resutlUrl;
	}
}
